package com.cheer.controller;

import com.cheer.common.Result;
import com.cheer.common.ResultEnum;
import com.cheer.entity.ExMatchUserEntity;
import com.cheer.param.SendInfoUpdateParam;
import com.cheer.param.UserUpdateParam;
import com.cheer.service.AuthService;
import com.cheer.service.ExAuthService;
import com.cheer.service.LotteryService;
import com.cheer.vo.*;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@Api(tags = "抽奖 API")
@RequestMapping("/lottery")
public class LotteryController {

    @Autowired
    private LotteryService lotteryService;

    @Autowired
    private AuthService authService;

    @Autowired
    private ExAuthService exAuthService;

    @ApiOperation("中奖轮播")
    @GetMapping("/announcement")
    public Result<List<String>> getLotteryAnnouncement(HttpSession session) {
        authService.getUserId(session);
        return Result.success(lotteryService.getLotteryAnnouncement());
    }

    @ApiOperation("奖品List")
    @GetMapping("/prizeList")
    public Result<List<PrizeListVO>> prizeList(HttpSession session) {
        authService.getUserId(session);
        List<PrizeListVO> list = lotteryService.prizeList();
        return Result.success(list);
    }

    @ApiOperation("抽奖页-用户信息 TODO")
    @GetMapping("/userInfo")
    public Result<LotteryUserInfoVO> userInfo(HttpSession session) {
        LotteryUserInfoVO lotteryUserInfoVO = lotteryService.userInfo(authService.getOpenid(session));
        return Result.success(lotteryUserInfoVO);
    }

    @ApiOperation("抽奖页-戳我兑换 TODO")
    @GetMapping("/exchange")
    public Result exchange(HttpSession session) {
        Integer ableExchangeTime = lotteryService.exchange(authService.getOpenid(session));
        return Result.success(ableExchangeTime);
    }

    @ApiOperation("抽奖页-立即抽奖 TODO")
    @GetMapping("/start")
    public Result<LotteryPrizeVO> startLottery(HttpSession session) {
        return Result.success(lotteryService.startLottery(session));
    }

    @ApiOperation(value = "中奖信息页-我的信息 TODO", notes = "原(GET)：match/auth/user/detail")
    @GetMapping("/user/detail")
    public Result<ExMatchUserVO> userDetail(HttpSession session) {

        ExMatchUserVO exMatchUserVO = exAuthService.info(authService.getExUserByOpenId(session));
        return Result.success(exMatchUserVO);
    }

    @ApiOperation(value = "中奖信息页-我的信息(更新) TODO", notes = "原(POST)：match/auth/user/detail")
    @PostMapping("/user/detail")
    public Result updateUser(@RequestBody @Valid UserUpdateParam userUpdateParam, HttpSession session) {
        boolean province = StringUtils.isNotBlank(userUpdateParam.getProvince());
        boolean city = StringUtils.isNotBlank(userUpdateParam.getCity());
        boolean region = StringUtils.isNotBlank(userUpdateParam.getRegion());
        boolean address = StringUtils.isNotBlank(userUpdateParam.getAddress());
        if (!((province && city && region && address)
                || (!province && !city && !region && !address))) {
            return Result.error(ResultEnum.SERVER_ERROR, "地址信息填写不完整");
        }
        exAuthService.updateUser(userUpdateParam, authService.getExUserByOpenId(session).getId());
        return Result.success();
    }

    @ApiOperation(value = "中奖信息页-获奖记录 TODO",notes = "原 match/lottery/record")
    @GetMapping("/record")
    public Result<LotteryRecordVO> getLotteryRecord(HttpSession session) {
        String prize = lotteryService.getLotteryRecord(authService.getExUserByOpenId(session).getId());
        ExMatchUserVO userBO = exAuthService.info(authService.getExUserByOpenId(session));
        LotteryRecordVO lotteryRecordVO = new LotteryRecordVO()
                .setPrize(prize);
        if (userBO != null) {
            lotteryRecordVO.setPhone(userBO.getPhone());
            if (StringUtils.isNotBlank(userBO.getProvince()) && StringUtils.isNotBlank(userBO.getCity())
                    && StringUtils.isNotBlank(userBO.getRegion()) && StringUtils.isNotBlank(userBO.getAddress())) {
                if (StringUtils.equals(userBO.getProvince(), userBO.getCity())) {
                    lotteryRecordVO.setAddress(StringUtils.join(Lists.newArrayList(userBO.getCity(),
                            userBO.getRegion(), userBO.getAddress()), " "));
                } else {
                    lotteryRecordVO.setAddress(StringUtils.join(Lists.newArrayList(userBO.getProvince(), userBO.getCity(),
                            userBO.getRegion(), userBO.getAddress()), " "));
                }
            }
        }
        return Result.success(lotteryRecordVO);
    }

    @ApiOperation(value = "中奖信息页-获奖记录 TODO", notes = "原（GET） match/auth/send-info")
    @GetMapping("/send-info")
    public Result<SendInfoVO> getSendInfo(HttpSession session) {
        SendInfoVO sendInfoVO = exAuthService.getSendInfo(authService.getExUserByOpenId(session));
        return Result.success(sendInfoVO);
    }

    @ApiOperation(value = "中奖信息页-获奖记录(更新) TODO", notes = "原（POST） match/auth/send-info")
    @PostMapping("/send-info")
    public Result updateSendInfo(@RequestBody @Valid SendInfoUpdateParam sendInfoUpdateDTO, HttpSession session) {
        exAuthService.updateSendInfo(sendInfoUpdateDTO, authService.getExUserByOpenId(session).getId());
        return Result.success();
    }
}
